package com.xsomnus.network_programming.netty.udp.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;

import java.nio.charset.StandardCharsets;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/24 0024 10:20
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
public class UdpClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
        String response = msg.content().toString(StandardCharsets.UTF_8);
        if (response.startsWith("query result")) {
            System.out.println(response);
            ctx.close();
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println(cause.getLocalizedMessage());
        ctx.close();
    }
}
